package api

import (
	"anban/logic"
	"anban/service"
	"github.com/astaxie/beego"
	"time"
)

type TimerTask struct {
	beego.Controller
}

func (c *TimerTask) Prepare() {
	c.EnableXSRF = false
}

// 检查学生是否按时到校或按时离校
func (c *TimerTask) CheckStudentSafe() {
	inputApiSecret := c.GetString("s")
	apiSecret := beego.AppConfig.String("anbanapisecret")
	if inputApiSecret != apiSecret {
		c.Ctx.WriteString("ok")
		return
	}

	var p map[string]interface{}
	curHour := time.Now().Hour()
	// 检查学生是否按时到校
	if curHour >= 0 && curHour <= 12 {
		p = map[string]interface{}{}
		p["status"] = 1
		_, schoolList := service.GetSchoolList(p)
		for _, school := range schoolList {
			go logic.CheckStudentGoSchool(school)
		}
	}
	// 检查学生是否按时离校
	if curHour > 12 && curHour <= 23 {
		p = map[string]interface{}{}
		p["status"] = 1
		_, schoolList := service.GetSchoolList(p)
		for _, school := range schoolList {
			go logic.CheckStudentLeaveSchool(school)
		}
	}

	c.Ctx.WriteString("ok")
}